/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MobileNode;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class Sender extends Thread {

    private DatagramSocket socket;
    private int port;
    private String pacote, ipAddress;
    private MobileNode mn;

    public Sender(MobileNode mn, int port, String ipAddress, String pacote) {
        //System.out.println("sender port: " + port);
        this.mn = mn;
        this.port = port;
        this.ipAddress = ipAddress;
        this.pacote = pacote;
        this.start();
    }

    @Override
    public void run() {
        try {
            socket = new DatagramSocket();
            byte[] mensage = pacote.getBytes();
            InetAddress host = InetAddress.getByName(ipAddress);
            DatagramPacket request = new DatagramPacket(mensage, mensage.length, host, port);
            socket.send(request);

        } catch (SocketException e) {
            System.out.println("Socket: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO: " + e.getMessage());
        }
    }
}
